let httpUtil = {};
const baseUrl = "http://evcssappapi.yushengkeji.cn/";  // 请求地址
httpUtil.baseUrl= baseUrl;

httpUtil.get= function({
			url,
			param,
			header
		}) {
			return new Promise(function(resolve, reject) {
				uni.request({
					url: baseUrl + url,
					data: param,
					method: "GET",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					success: function(res) {
						if (res.statusCode !== 200) {
							reject(res);
						} else {
							resolve(res);
						}
					},
					fail: function(err) {
						reject(err);
					}
				})
			}).catch(err => {})
		};
		httpUtil.post= function({
			url,
			data,
			header
		}) {
			return new Promise(function(resolve, reject) {
				uni.showLoading({title:'加载中'});
				uni.request({
					url: baseUrl + url,
					data: data,
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					success: function(res) {
						console.log("res="+JSON.stringify(res));
						uni.hideLoading();
						if (res.statusCode !== 200) {
							reject(res)
						} else {
							resolve(res);
						}
					},
					fail: function(err) {
						reject(err)
					}
				})
			}).catch(err => {
				console.log('catch:',err);
			})
		};
		httpUtil.postJSON=function({
			url,
			data,
			header
		}) {
			return new Promise(function(resolve, reject) {
				uni.showLoading({title:'加载中'});
				uni.request({
					url: baseUrl + url,
					data: data,
					method: "POST",
					dataType:'JSON',
					header: {
						'Content-Type': 'application/json;charset=UTF-8'
					},
					success: function(res) {
						uni.hideLoading()
						if (res.statusCode !== 200) {
							reject(res)
						} else {
							resolve(res);
						}
					},
					fail: function(err) {
						reject(err)
					}
				})
			}).catch(err => {
				console.log('catch:',err);
			})
		};
		httpUtil.upImg= function({
			url,
			imgUrl
		}) {
			return new Promise(function(resolve, reject) {
				uni.uploadFile({
					url: "baseUrl" + url,
					filePath: imgUrl,
					name: 'file',
					success: function(uploadFileRes) {
						resolve(uploadFileRes);
					},
 
					fail: function(err) {
						reject(err)
					}
				});
			})
		};
		httpUtil.tokenreq=function({
			url,
			data
		}) {
			//此token是登录成功后后台返回保存在storage中的
			//let token ="";
			// if(uni.getStorageSync('token')){
			// 	token = uni.getStorageSync('token');
			// }
			// if(!token){
			// 	return;
			// }
			return new Promise(function(resolve, reject) {
				uni.showLoading({title:'加载中'});
				uni.request({
					url: baseUrl + url,
					data: data,
					method: "POST",
					dataType:'JSON',
					header: {
					    'Authorization': "Bearer " + uni.getStorageSync('token') //自定义请求头信息
					},
					success: function(res) {
						uni.hideLoading();
						if (res.statusCode !== 200) {
							if(res.statusCode == 401)
							{
									console.log('statusCode:',res.statusCode);
									  uni.redirectTo({url:'/pages/user/login/login?logintype=my'});
							}else {
							reject(res)
							}
						} else {
							resolve(res);
						}
					},
					fail: function(err) {
						reject(err)
					}
				})
			}).catch(err => {
				console.log('catch:',err);
			})
		};
		httpUtil.zf=function({
			url,
			data
		}) {
			return new Promise(function(resolve, reject) {
				uni.request({
					url: baseUrl + url,
					data: data,
					method: "POST",
					dataType:'JSON',
					header: {
					    'Authorization': "Bearer " + uni.getStorageSync('token') //自定义请求头信息
					},
					success: function(res) {
						if (res.statusCode !== 200) {
							reject(res)
						} else {
							resolve(res);
						}
					},
					fail: function(err) {
						reject(err)
					}
				})
			}).catch(err => {
				console.log('catch:',err);
			})
		};
		
	
module.exports = httpUtil;
